﻿// stock application

// Array data for the grid test
Ext.grid.dummyData = [
    ['sh600013','民生银行',8.094,7.250,-10.427,'2010-01-28 17:15:27','买入'],
    ['sz000031','中粮地产',10.124,9.440,-6.756,'2010-01-13 17:15:27','买入']
];


var _stock_store = new Ext.data.JsonStore({
    // store configs
    autoLoad:true,
    url: '/Data/ChinaStockData.json',
    storeId: 'myStore',
    idProperty: 'code',
    // reader configs
    root: 'rows',
    totalProperty: 'count',
    fields: ['name', 'code']
});

//股票买卖纪录录入窗口
var _stockWinModule = Ext.extend(Ext.app.Module, {
        id:'stock-win',
        init : function(){
            this.launcher = {
                text: '股票证券',
                iconCls:'icon-grid',
                handler : this.createWindow,
                scope: this
            }
        },
        createWindow : function(desktop){
            var win = desktop.getWindow('stock-win');
            if(!win){
                win = desktop.createWindow({
                        id: 'stock-win',
                        title:'添加纪录',
                        width:400,
                        height:300,
                        iconCls: 'icon-grid',
                        shim:false,
                        animCollapse:false,
                        constrainHeader:true,
                        maximizable:false,
                        resizable:false,                        
                        layout: 'fit',
                        items:new Ext.FormPanel({
                                    labelWidth: 60, 
                                    url:'save-form.php',
                                    frame:true,
                                    bodyStyle:'padding:5px 5px 0',              
                                    layout:'column',
                                    items: [{
                                        layout:'form',
                                        columnWidth: .5,
                                        defaults:{width:100},
                                        items:[{
                                            fieldLabel: '股票代码',
                                            name: 'stock_code',
                                            xtype:'combo',
                                            store:_stock_store,
                                            mode: 'local',
                                            displayField:'code',
                                            listeners:{
                                                change:function(field,newValue,oldValue ){
                                                    if(field.getRawValue() == newValue){
                                                        alert('无效代码');
                                                    }else{
                                                        Ext.getCmp('txt_stockName').setValue(newValue);
                                                    }
                                                }
                                            },
                                            valueField:'name',
                                            allowBlank:false
                                            },{
                                            fieldLabel: '股票名称',
                                            id:'txt_stockName',
                                            xtype:'textfield',  
                                            readOnly:true,
                                            name: 'stock_name'
                                            },{
                                            fieldLabel: '价格',
                                            xtype:'textfield',
                                            name: 'stock_price'
                                            }]
                                        },{
                                        layout:'form',
                                        columnWidth: .5,
                                        defaults:{width:100},
                                        items:[{
                                            fieldLabel: '数量',
                                            xtype:'textfield',
                                            name: 'stock_count'
                                            },{
                                            fieldLabel: '交易时间',
                                            name: 'stock_time',
                                            editable:false,
                                            xtype: 'datefield'
                                            },{
                                            fieldLabel: '交易类型',
                                            name: 'stock_type',
                                            xtype:'combo',
                                            store:['买入','卖出'],
                                            value:'买入',
                                            editable:false,
                                            triggerAction:'all'
                                            }]
                                        },{
                                            xtype:'fieldset',
                                            title: '附加费用', 
                                            columnWidth: 1,
                                            collapsed: false, 
                                            collapsible: true,
                                            autoHeight:true,
                                            defaults:{layout:'form',columnWidth: .5,defaults:{width:100,xtype:'textfield'}},
                                            padding:5,
                                            layout:'column',
                                            items :[{
                                                items:[{
                                                    fieldLabel: '印花税率',
                                                    value:0.001,
                                                    name: 'stock_yhsl'
                                                    },{
                                                    fieldLabel: '佣金比例',
                                                    value:0.001,
                                                    name: 'stock_yjl'
                                                    },{
                                                    fieldLabel: '过户费', 
                                                    value:1,                    
                                                    name: 'stock_ghf'
                                                    }]
                                        },{
                                            items:[{
                                                fieldLabel: '印花税费',
                                                name: 'stock_yhs'
                                                },{
                                                fieldLabel: '佣金费',
                                                name: 'stock_yj'
                                                },{
                                                fieldLabel: '附加费',
                                                name: 'stock_fjf'
                                                }]
                                        }]
                                        },{
                                        columnWidth: .5,
                                        padding:5,
                                        layout:'form',
                                        items:{
                                            width:100,
                                            fieldLabel: '费用小计',
                                            xtype:'textfield',
                                            name: 'stock_fyprice'
                                         }
                                         },{
                                         columnWidth: .5,
                                         padding:5,
                                         layout:'form',
                                         items:{
                                         width:100,
                                         fieldLabel: '平均价格',
                                         name: 'stock_agvPrice',
                                         xtype:'textfield'
                                         }}],

                                    buttons: [{
                                        text: '计算'                                        
                                    },{
                                        text: '保存'
                                    }]
                                })
                            
                    });
            }
            win.show();
        }
    });
var _stockWin = new _stockWinModule();
                    
MyDesktop.StockGridWindow = Ext.extend(Ext.app.Module, {
    id:'stock-grid-win',
    init : function(){
        this.launcher = {
            text: '股票证券',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('stock-grid-win');
        if(!win){
            win = desktop.createWindow({
                id: 'stock-grid-win',
                title:'股票证券',
                width:740,
                height:480,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                layout: 'fit',
                items:
                    new Ext.grid.GridPanel({
                        border:false,
                        ds: new Ext.data.Store({
                            reader: new Ext.data.ArrayReader({}, [
                               {name: '股票代码'},
                               {name: '股票名称'},
                               {name: '成本价', type: 'float'},
                               {name: '现价', type: 'float'},
                               {name: '盈亏比例', type: 'float'},
                               {name: '交易日期', type: 'date',dateFormat:'Y-m-d H:i:s'},
                               {name: '操作类型'}
                            ]),
                            data: Ext.grid.dummyData
                        }),
                        cm: new Ext.grid.ColumnModel([
                            new Ext.grid.RowNumberer(),
                            {header: "股票代码", width: 100, sortable: true, dataIndex: '股票代码'},
                            {header: "股票名称", width: 100, sortable: true, dataIndex: '股票名称'},
                            {header: "成本价", width: 70, sortable: true, renderer: Ext.util.Format.usMoney, dataIndex: '成本价'},
                           {header: "现价", width: 70, sortable: true, renderer: Ext.util.Format.usMoney, dataIndex: "现价"},
                            {header: "% 盈亏比例", width: 70, sortable: true, dataIndex: '盈亏比例'},
                            {header: "交易日期", width: 70, sortable: true, dataIndex: '交易日期',renderer: Ext.util.Format.dateRenderer('Y-m-d')},
                            {header: "操作类型", width: 70, sortable: true, dataIndex: '操作类型'}
                        ]),
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'添加新纪录',
                            tooltip:'添加一条新的买卖纪录',
                            iconCls:'add',
                            handler: function(){
                                _stockWin.createWindow(desktop);
                            }
                        }, '-',{
                            text:'删除纪录',
                            tooltip:'删除一条买卖纪录',
                            iconCls:'remove',
                            handler: function(){
                                //TODO:remove record
                                alert(_stock_store.getCount())
                            }
                        }]
                    })
            });
        }
        win.show();
    }
});

//将应用程序添加入桌面
MyDesktop._modules.push(new MyDesktop.StockGridWindow());